package com.xiaoxin.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxin.lease.common.exception.CheckExitException;
import com.xiaoxin.lease.common.exception.LeaseException;
import com.xiaoxin.lease.common.result.ResultCodeEnum;
import com.xiaoxin.lease.model.entity.*;
import com.xiaoxin.lease.model.enums.ItemType;
import com.xiaoxin.lease.model.enums.ReleaseStatus;
import com.xiaoxin.lease.web.admin.mapper.ApartmentInfoMapper;
import com.xiaoxin.lease.web.admin.service.*;
import com.xiaoxin.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.xiaoxin.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.xiaoxin.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.xiaoxin.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.xiaoxin.lease.web.admin.vo.fee.FeeValueVo;
import com.xiaoxin.lease.web.admin.vo.graph.GraphVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
@Slf4j
@Transactional
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {




    @Autowired
    ApartmentFacilityService  apartmentFacilityService;

    @Autowired
    ApartmentLabelService apartmentLabelService;

    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    RoomInfoService roomInfoService;





    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentInfoVo) {
        //保存公寓信息到apartment_info表
        Long id = apartmentInfoVo.getId();
        boolean flag = id!=null;
        //保存公寓信息
        super.saveOrUpdate(apartmentInfoVo);
        if(flag){
            //如果id不为null 代表进行更新操作
            //1、更新公寓信息
          /**  LambdaQueryWrapper<ApartmentInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApartmentInfo::getId, id);
            apartmentInfoService.remove(queryWrapper);*/

            //2、更新配套信息
            LambdaQueryWrapper<ApartmentFacility> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ApartmentFacility::getApartmentId, id);
            apartmentFacilityService.remove(queryWrapper1);

            //3、更新标签信息
            LambdaQueryWrapper<ApartmentLabel> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(ApartmentLabel::getApartmentId, id);
            apartmentLabelService.remove(queryWrapper2);

            //4、更新杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(ApartmentFeeValue::getApartmentId, id);
            apartmentFeeValueService.remove(queryWrapper3);

            //更新图片信息
            LambdaQueryWrapper<GraphInfo> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(GraphInfo::getItemId, id);
            graphInfoService.remove(queryWrapper4);
        }

        Long id1 = apartmentInfoVo.getId();

        //保存配套信息

        apartmentInfoVo.getFacilityInfoIds().forEach(facilityInfoId -> {
            ApartmentFacility apartmentFacility = new ApartmentFacility();
            apartmentFacility.setFacilityId(facilityInfoId);
            apartmentFacility.setApartmentId(id1);
            apartmentFacilityService.saveOrUpdate(apartmentFacility);

        });

        //保存标签信息apartment_label
        apartmentInfoVo.getLabelIds().forEach(labelId -> {
            ApartmentLabel apartmentLabel = new ApartmentLabel();
            apartmentLabel.setApartmentId(id1);
            apartmentLabel.setLabelId(labelId);
            apartmentLabelService.saveOrUpdate(apartmentLabel);
        });

        //保存杂费信息apartment_fee_value
        apartmentInfoVo.getFeeValueIds().forEach(feeValueId -> {
            ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
            apartmentFeeValue.setApartmentId(id1);
            apartmentFeeValue.setFeeValueId(feeValueId);
            apartmentFeeValueService.saveOrUpdate(apartmentFeeValue);
        });

        //保存图片信息graph_info
        List<GraphVo> graphInfoList =  apartmentInfoVo.getGraphVoList();
        List<GraphInfo> graphInfos = new ArrayList<>();
        for(GraphVo graphVo : graphInfoList){
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(id1);
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);

    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page,queryVo);
    }

    @Override
    public  ApartmentDetailVo selectApaertInfoVoById(Long apartmentId) {
        //通过id获取公寓详情
        //1、获取公寓详情
        ApartmentInfo apartmentInfoList =  this.getById(apartmentId);
        /**
         * 如果公寓存在，再继续查找标下面信息，否则就返回空
         */
        if(apartmentInfoList == null){
            throw new CheckExitException(ResultCodeEnum.FAIL);
        }else{
            ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
            //2、获取标签详情
            List<FacilityInfo> facilityInfoList =  apartmentFacilityService.selectApartmentFacilityById(apartmentId);
            //3、获取配套详情
            List<LabelInfo>labelInfoList =  apartmentLabelService.selectLabelByApartmentId(apartmentId);
            //4、获取杂费
            List<FeeValueVo> feeValueVoList =  apartmentFeeValueService.selectFeeValueByApartmentId(apartmentId);
            //5、获取图片
            List<GraphVo> graphVoList = graphInfoService.selectGraphInfoByApartmentId(apartmentId);

            BeanUtils.copyProperties(apartmentInfoList,apartmentDetailVo);
            apartmentDetailVo.setFacilityInfoList(facilityInfoList);
            apartmentDetailVo.setLabelInfoList(labelInfoList);
            apartmentDetailVo.setFeeValueVoList(feeValueVoList);
            apartmentDetailVo.setGraphVoList(graphVoList);
            return apartmentDetailVo;

        }
    }

    /**
     * 根据ID删除相关信息
     * @param id
     */
    @Override
    public void removeByIdApartmentInfo(Long id) {
        //执行删除之前需确认该公寓下是否有房间，如果有，禁止删除
        Long count =  roomInfoService.count(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId,id));
        if(count > 0){
           throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //1、删除公寓信息
        super.removeById(id);

        //2、删除公寓和配套中间表
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        //3、删除公寓和配套中间表
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        //4、删除公寓和杂费中间表
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
        //5、删除公寓图片信息表
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id));
    }

    @Override
    public void updateIsRease(Long id, ReleaseStatus status) {
        super.update(new LambdaUpdateWrapper<ApartmentInfo>().eq(ApartmentInfo::getId,id).set(ApartmentInfo::getIsRelease,status));
    }

    @Override
    public List<ApartmentInfo> getApartmentInfo(Long id) {
        return         super.list(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getDistrictId,id));

    }
}




